myApp.constant('addImgUpload',admin_url+'OssPic/get');
myApp.constant('removeImgUpload',admin_url+'OssPic/delosspic');
myApp.factory('$serImgUpload', function ($q,$http,$serAccessSignin,addImgUpload,removeImgUpload,FileUploader,IMGPATH) {
    var host,policyBase64,accessid,signature,callbackbody,key,expire = 0,now,timestamp;
    now = timestamp = Date.parse(new Date()) / 1000;
    function curName() {
        var d = new Date();
        var year = d.getFullYear()+"";
        var month = d.getMonth() + 1;
        var date = d.getDate();
        var day = d.getDay();
        var Hours=d.getHours(); //获取当前小时数(0-23)
        var Minutes=d.getMinutes(); //获取当前分钟数(0-59)
        var Seconds=d.getSeconds(); //获取当前秒数(0-59)
        var curDateTime = year;
        if (month > 9){curDateTime = curDateTime + month;
        }else{curDateTime = curDateTime + "0" + month;
        }if (date > 9){curDateTime = curDateTime + date;
        } else{curDateTime = curDateTime + "0" + date;
        }if (Hours > 9){curDateTime = curDateTime + Hours;
        } else{curDateTime = curDateTime + "0" + Hours;
        }if (Minutes > 9){curDateTime = curDateTime + Minutes;
        } else {curDateTime = curDateTime + "0" + Minutes;
        }if (Seconds > 9){curDateTime = curDateTime + Seconds;
        } else{ curDateTime = curDateTime + "0" + Seconds;}

        var rnd="";
        for(var i=0;i<4;i++){
            rnd+=Math.floor(Math.random()*10);
        }
        return curDateTime+'_'+rnd;
    }
    return {
        setImgUpload:function(prefix,auto,fn){
            var uploader = new FileUploader();
            uploader.onAfterAddingFile = function(fileItem) {
                fileItem.file.alias = fileItem.file.name;
                fileItem.file.name =prefix + curName() + fileItem.file.name.match(/\.\w*?$/);
                if(!!fn&&angular.isFunction(fn)){
                    fn(fileItem)
                }
                if(auto){
                    uploader.uploadAll();
                }
            };



            return uploader;
        },
        putImgUpload:function(item,url,fn){
            now = timestamp = Date.parse(new Date()) / 1000;
            //console.log($serAccessSignin.getUser(),$serAccessSignin.getUser()&&$serAccessSignin.getUser().token);
            //var urltext = addImgUpload+'?dir='+IMGPATH[url]+"&"+'?token='+$serAccessSignin.getUser()&&$serAccessSignin.getUser().token;
            //console.log('?token=',$serAccessSignin.getUser()&&$serAccessSignin.getUser().token);
            var urltext = addImgUpload+'?dir='+IMGPATH[url];
            var tokenNum = $serAccessSignin.getUser()&&$serAccessSignin.getUser().token;
            //console.log(tokenNum);
            //console.log('urltext',urltext)
            $http.get(urltext+'&token='+tokenNum).then(function(res){
                var obj = res.data;
                host = obj['host'];
                policyBase64 = obj['policy'];
                accessid = obj['accessid'];
                signature = obj['signature'];
                expire = parseInt(obj['expire']);
                callbackbody = obj['callback'];
                key = angular.copy(IMGPATH[url]);
                uploadOSS();
            });
            function uploadOSS(){
                item.onBeforeUploadItem =function(item){
                    item.url = host;
                    item.formData = [
                        {
                            'key' : key + '${filename}',
                            'policy': policyBase64,
                            'OSSAccessKeyId': accessid,
                            'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
                            'callback' : callbackbody,
                            'signature': signature
                        }
                    ]
                };
                if(!!fn&&angular.isFunction(fn)){
                    fn();
                }
            }
        },
        upload:function(item){
            if(!!item.url){
                item.uploadAll();
            }
        },
        removeImgUpload:function(name,url){
            return $http({
                method: 'POST',
                url: removeImgUpload,
                data : {
                    dir:angular.copy(IMGPATH[url]),
                    picName:name
                }
            }).then(function(res){
                switch (res.data.code){
                    case 1:return res.data;
                    case 0:
                    case -1:return res.data.message;
                }
            });
        },
        removeDelEditorAll:function(editorImg,url,challback){
            var fileNewAllNum = editorImg.queue;
            var imgAll = document.getElementsByClassName('editor-image');
            function removeImg(names){
                $http({
                    method: 'POST',
                    url: removeImgUpload,
                    data : {
                        dir:angular.copy(IMGPATH[url]),
                        picName:names
                    }
                }).then(function(res){
                    if(challback||angular.isFunction(challback)){challback();}
                });
            }
        },
        editorImgReplace:function(idName,url,file){
            $('#'+idName).attr('src',admin_img_url+IMGPATH[url]+file.file.name).attr('data-update',true);
        }
    }
});
